#include "serialplot.h"
#include <QSerialPortInfo>
#include <QDebug>
#include<QFile>
#include<QFileInfo>

SerialPlot::SerialPlot(QObject *parent) : QObject(parent)
{
    serialScanTime = new QTimer(this);
    serialPort = new QSerialPort(this);

    serialScanTime->start(5000);
    connect(serialScanTime,&QTimer::timeout,this,&SerialPlot::ScanTimecallback);
    connect(serialPort,&QSerialPort::readyRead,this,&SerialPlot::readcallback);

    connect(serialPort,static_cast<void (QSerialPort::*)
            (QSerialPort::SerialPortError)>(&QSerialPort::error),this,&SerialPlot::errorcallback);
}
SerialPlot::~SerialPlot()
{
    serialScanTime->stop();

}

//扫描串口
void SerialPlot::ScanTimecallback(){
    if(serialconnect == true)
        return;
    QStringList serialPortName;
    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts()){
        serialPortName << info.portName();
        qDebug() << info.portName();
    }
    emit ScanTimeSignals(serialPortName);
}

//连接串口
void SerialPlot::connectCOM(COMINFO info){
    QSerialPort::BaudRate baudRate = QSerialPort::BaudRate::Baud115200;
    QSerialPort::DataBits dataBits = QSerialPort::DataBits::Data8;
    QSerialPort::Parity   parity = parity = QSerialPort::Parity::NoParity;
    QSerialPort::StopBits stopBits = stopBits = QSerialPort::StopBits::OneStop;

    if( info.baud == 115200)
        baudRate = QSerialPort::BaudRate::Baud115200;
    else if( info.baud == 9600)
        baudRate = QSerialPort::BaudRate::Baud9600;
    else
        baudRate = QSerialPort::BaudRate::Baud115200;;


    if(info.dataBits == 8)
        dataBits = QSerialPort::DataBits::Data8;
    else if(info.dataBits == 7)
        dataBits = QSerialPort::DataBits::Data7;
    else if(info.dataBits == 6)
        dataBits = QSerialPort::DataBits::Data6;
    else
        dataBits = QSerialPort::DataBits::Data5;


    if(info.parity == "无")
        parity = QSerialPort::Parity::NoParity;
    else if( info.parity == "奇校验" )
        parity = QSerialPort::Parity::OddParity;
    else
        parity = QSerialPort::Parity::EvenParity;

    if(info.stopBits == 1)
        stopBits = QSerialPort::StopBits::OneStop;
    else if(info.stopBits == 1.5)
        stopBits = QSerialPort::StopBits::OneAndHalfStop;
    else
        stopBits = QSerialPort::StopBits::TwoStop;


    serialPort->setPortName(info.name);
    serialPort->setBaudRate(baudRate);
    serialPort->setDataBits(dataBits);
    serialPort->setParity(parity);
    serialPort->setStopBits(stopBits);
    if(serialPort->open(QIODevice::ReadWrite)){
        serialconnect = true;
            qDebug() << info.name;
    }else{
         serialconnect = false;
             qDebug() << info.name;
    }
    emit serialState(serialconnect);
}

//关闭串口
void SerialPlot::closeCOM(){
    serialconnect = false;
    emit serialState(serialconnect);
    serialPort->close();
}

//串口出现错误
void SerialPlot::errorcallback(QSerialPort::SerialPortError error){
    if (error == QSerialPort::ResourceError||error== QSerialPort::PermissionError)
    {
        serialconnect = false;
        serialPort->close();
        emit serialState(serialconnect);
    }
}

void SerialPlot::displayformat(bool hex_en){
    hex_display = hex_en;
}

void SerialPlot::send(char *data,int len){
    if( serialconnect == false)
        return;
    serialPort->write(data,len);
}

#include<QTime>
#include<QCoreApplication>
void SerialPlot::sendFile(QString filename,int pageSize,int delay){
    QFile file(filename);
    file.open(QIODevice::ReadOnly);

    char databuff[1024];
    int sendSize = 0;
    while(1){
       qint64 realSize = file.read(databuff,pageSize);
       sendSize += realSize;
       serialPort->write(databuff,pageSize);
       qDebug() << realSize;
       emit sendFileCSize(sendSize);
       if( realSize < pageSize)
           break;
      // _sleep(delay);
       //延时 非阻塞
       QTime _Timer = QTime::currentTime().addMSecs(delay);
       while( QTime::currentTime() < _Timer )
           QCoreApplication::processEvents(QEventLoop::AllEvents, 5);
    }
    QFileInfo base(filename);
    qDebug() << "文件大小 "<<  base.size();
    qDebug() << "发送大小 " << sendSize;
    file.close();
}


void SerialPlot::readcallback(){
   QByteArray bytearray = serialPort->readAll();
   QString hex_string ="";
   char *hex = bytearray.data();
   for(int i =0;i<bytearray.size();i++){
       hex_string += (QString("%1 ").arg((unsigned)hex[i],0,16));
   }
   emit readSignals(bytearray,hex_string);
}






